<?php

namespace zhtfrontend\controllers;

use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use common\components\services\customer\Account;
use common\components\services\orders\Oper;
use common\components\services\orders\ZhtOper;
use common\models\datas\Customer;
use common\models\datas\AccountRecord;
use common\models\datas\Student;
use doumiaoduo\buildform\BuildForm;
use moonland\phpexcel\Excel;
use yii\helpers\Html;
use zhtfrontend\components\helpers\Funs;
use zhtfrontend\components\services\commission\MechanicCommission;
use zhtfrontend\models\datas\ArtTeaching;
use zhtfrontend\models\datas\ClassLesson;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassManageGoods;
use zhtfrontend\models\datas\ClassStudent;
use common\models\datas\DepositLog;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsAttr;
use zhtfrontend\models\datas\LessonStudent;
use zhtfrontend\models\datas\Mechanic;
use zhtfrontend\models\datas\Orders;
use zhtfrontend\models\datas\OrdersAddress;
use zhtfrontend\models\datas\OrdersItems;
use zhtfrontend\models\datas\OrdersItemsRelation;
use zhtfrontend\models\datas\OrdersLog;
use zhtfrontend\models\datas\OrdersType;
use yii\base\Exception;
use zhtfrontend\models\datas\StudentCheck;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\data\Pagination;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;

class GoodsordersController extends \zhtfrontend\components\bases\BaseController
{

    public function actionIndex()
    {
        $mid = Fun::getMechanicIds();
        $param['searchData'] = \Yii::$app->request->get();
            $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
            $data = Orders::find()->where(['order_type'=>'goods']);
            if (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'types') {
                $data->andWhere(['type_id'=>\Yii::$app->user->identity->types_id]);
            }
            $data->orderBy('id DESC');

            if (!empty($param['searchData'])) {
                $search_data = array_filter($param['searchData']);
                if (!empty($search_data['year']) && !empty($search_data['mouth'])) {
                    if ($search_data['types'] == 2) {
                        $time = $search_data['year'] . "-" . $search_data['mouth'];
                        $end = Fun::getCurMonthLastDay($time);
                        $data->andWhere(['<=', 'created_at', $end]);
                    } else {
                        $time = $search_data['year'] . "-" . $search_data['mouth'];
                        $start = Fun::getCurMonthFirstDay($time);
                        $end = Fun::getCurMonthLastDay($time);
                        $data->andWhere(['between', 'created_at', $start, $end]);
                    }
                }

                if (!empty($search_data['name'])) {
                    $customer_ids = Customer::find()->select('id')->where(['like', 'name', $search_data['name']])->andWhere(['site_id' => Fun::getSiteId()])->column();
                    $data->andWhere(['customer_id' => $customer_ids]);
                }
                if (!empty($search_data['mobile'])) {
                    $data->andWhere(['like', 'telephone', $search_data['mobile']]);
                }
                if (!empty($search_data['sn'])) {
                    $data->andWhere(['like', 'sn', $search_data['sn']]);
                }
                if (!empty($search_data['mechanic_id'])) {
//                    $orders_ids = OrdersItems::find()->select('orders_id')->where(['mechanic_id' => $search_data['mechanic_id']])->column();
                    $data->andWhere(['machanic_id' => $search_data['mechanic_id']]);
                }
            }
            if ($mid) {
//                $oids = OrdersItems::find()->select('orders_id')->where(['mechanic_id' => $mid])->column();
                $data->andWhere(['mechanic_id' => $mid]);
            }
            if (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'class_director') {
                $DirectorMechanicIds = Fun::getDirectorMechanicIds();
//                $oids = OrdersItems::find()->select('orders_id')->where(['mechanic_id' => $DirectorMechanicIds])->column();
                $data->andWhere(['mechanic_id' => $DirectorMechanicIds]);
            }
            $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
            isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
            $model = $data->offset($pages->offset)->limit($pages->limit)->all();
            $array = Fun::asArray($model, ['customer_name', 'status_name', 'operate', 'ship_status_name', 'pay_status_name', 'payment_name']);

//        $mechanic_ids = OrdersItems::find()->select('mechanic_id')->joinWith('orderss')->groupBy('mechanic_id')->column();
        $mechanic_ids = Orders::find()->select('mechanic_id')->groupBy('mechanic_id')->column();
        $mechanicModel = Mechanic::find()->where(['id' => $mechanic_ids])->orderBy('id DESC');
        if ($mid) {
            $mechanicModel->where(['id' => $mid]);
        }
        $mechanic = $mechanicModel->asArray()->all();
        $data_mechanic = ArrayHelper::map($mechanic, 'id', 'name');
        $data_mechanic[0] = '请选择';
        return $this->render('index', ['mechanic' => $data_mechanic,'data'=>$array,'pages'=>$pages]);
    }

    /**
     *  详情
     */
    public function actionDetails()
    {
        $id = \Yii::$app->request->get('id');
        $model = Orders::findOne($id);
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $is_start = 0;
        $OrdersItems = OrdersItems::find()->where(['orders_id' => $id])->all();
        $orderitems = Fun::asArray($OrdersItems, ['status_name', 'type_name', 'goods_type_id', 'mechanic_name', 'total', 'student_name', 'class_status', 'check_number', 'is_start', 'is_was']);
        $orders = Fun::asArray([$model], ['customer_created_at', 'card_id', 'customer_name', 'status_name', 'pay_status_name', 'ship_status_name', 'appointment_name', 'payment_name', 'appointment_name']);
        $addr = OrdersAddress::find()->where(['orders_id' => $id])->one();
        return $this->render('details', ['order' => $orders, 'orderitem' => $orderitems, 'addr' => $addr, 'is_start' => $is_start]);
    }




}